<?php
namespace app\system\controller;

use app\common\model\ShopGoodsGevalModel;
use think\Request;

class ShopGoodsGevalController extends BaseController
{

    private $model;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->model = new ShopGoodsGevalModel();
    }

    /**
     * 评价首页
     * @return mixed
     *
     */
    public function index()
    {
        $query['status'] = $this->request->param('status', -1);
        if (in_array($query['status'], [0, 1])) {
            $map['geval_status'] = $query['status'];
        }
        if ($query['status'] == 2) {
            $map['geval_remark'] = '';
        }
        if ($query['status'] == 3) {
            $map['geval_remark'] = ['neq', ''];
        }
        $query['field']   = $this->request->param('field');
        $query['keyword'] = $this->request->param('keyword');
        if (!empty($query['field']) && !empty($query['keyword'])) {
            $map[$query['field']] = ['like', '%' . $query['keyword'] . '%'];
        }
        $list = $this->model->where('site_id', $this->getSiteId())->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('query', $query);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 查看回复
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            if (!empty($data['geval_remark'])) {
                $data['reply_time'] = time();
            }
            if ($this->model->editData($data)) {
                $this->success('修改成功！', url('index'));
            } else {
                $this->error($this->model->getError());
            }
        }
        $gevalId = $this->request->param('geval_id');
        $info    = $this->model->get($gevalId);
        $this->assign('info', $info);
        return $this->fetch();
    }

}